import axios from 'axios'
import env from '../../build/env'

let util = {

}
util.title = function (title) {
  title = title || '壹键通微课学习服务平台'
  window.document.title = title
}

const ajaxUrl = env === 'development' ? 'http://127.0.0.1:8888' : env === 'production' ? 'https://www.url.com' : 'https://debug.url.com'

util.ajax = axios.create({
  baseURL: ajaxUrl,
  timeout: 30000
})

util.oneOf = function (ele, targetArr) {
  if (targetArr.indexOf(ele) >= 0) {
    return true
  } else {
    return false
  }
}

util.showThisRoute = function (itAccess, currentAccess) {
  if (typeof itAccess === 'object' && itAccess.isArray()) {
    return util.oneOf(currentAccess, itAccess)
  } else {
    return itAccess === currentAccess
  }
}

util.handleTitle = function (vm, item) {
  return item.title
}

util.setCurrentPath = function (vm, name) {
  let currentPathArr = [{
    title: '首页',
    path: '/admin',
    name: 'dashboard'
  }, {
    title: 'dashboard',
    path: '/admin/dashboard',
    name: 'dashboard'
  }]
  vm.$store.commit('setCurrentPath', currentPathArr)

  return currentPathArr
}

var mimeData = '' +
  'application/msword,doc dot|' +
  'application/pdf,pdf|' +
  'application/pgp-signature,pgp|' +
  'application/postscript,ps ai eps|' +
  'application/rtf,rtf|' +
  'application/vnd.ms-excel,xls xlb|' +
  'application/vnd.ms-powerpoint,ppt pps pot|' +
  'application/zip,zip|' +
  'application/x-shockwave-flash,swf swfl|' +
  'application/vnd.openxmlformats-officedocument.wordprocessingml.document,docx|' +
  'application/vnd.openxmlformats-officedocument.wordprocessingml.template,dotx|' +
  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,xlsx|' +
  'application/vnd.openxmlformats-officedocument.presentationml.presentation,pptx|' +
  'application/vnd.openxmlformats-officedocument.presentationml.template,potx|' +
  'application/vnd.openxmlformats-officedocument.presentationml.slideshow,ppsx|' +
  'application/x-javascript,js|' +
  'application/json,json|' +
  'audio/mpeg,mp3 mpga mpega mp2|' +
  'audio/x-wav,wav|' +
  'audio/x-m4a,m4a|' +
  'audio/ogg,oga ogg|' +
  'audio/aiff,aiff aif|' +
  'audio/flac,flac|' +
  'audio/aac,aac|' +
  'audio/ac3,ac3|' +
  'audio/x-ms-wma,wma|' +
  'image/bmp,bmp|' +
  'image/gif,gif|' +
  'image/jpeg,jpg jpeg jpe|' +
  'image/photoshop,psd|' +
  'image/png,png|' +
  'image/svg+xml,svg svgz|' +
  'image/tiff,tiff tif|' +
  'text/plain,asc txt text diff log|' +
  'text/html,htm html xhtml|' +
  'text/css,css|' +
  'text/csv,csv|' +
  'text/rtf,rtf|' +
  'video/mpeg,mpeg mpg mpe m2v|' +
  'video/quicktime,qt mov|' +
  'video/mp4,mp4|' +
  'video/rmvb,rmvb rm|' +
  'video/asf,asf|' +
  'video/m2ts,m2ts|' +
  'video/mov,mov|' +
  'video/x-m4v,m4v|' +
  'video/x-flv,flv|' +
  'video/x-ms-wmv,wmv|' +
  'video/avi,avi|' +
  'video/webm,webm|' +
  'video/3gpp,3gpp 3gp|' +
  'video/3gpp2,3g2|' +
  'video/vnd.rn-realvideo,rv|' +
  'video/ogg,ogv|' +
  'video/x-matroska,mkv|' +
  'application/vnd.oasis.opendocument.formula-template,otf|' +
  'application/octet-stream,exe'

var mimeTypes = mimeData.split('|')
var extensions = {}

mimeTypes.forEach(mimeType => {
  var items = mimeType.split(',')
  items[1].split(' ').forEach(extension => {
    extensions[extension] = items[0]
  })
})

util.getOfficeDocumentExtensions = function () {
  return [
    'doc',
    'docx',
    'xls',
    'xlsx',
    'pptx',
    'ppt',
    'pdf'
  ]
}

util.getVideoExtensions = function () {
  return [
    'm2ts',
    'rm',
    'rmvb',
    'asf',
    'mpg',
    'avi',
    'mpeg',
    'mp4',
    '3gp',
    'mov',
    'wmv',
    'mkv'
  ]
}

util.contentType = function (extension) {
  if (extension) extension = extension.toLowerCase()
  return extensions[extension] || 'application/octet-stream'
}

util.liveStatusList = [{
  text: '待开始',
  value: 'pending'
}, {
  text: '正在直播',
  value: 'running'
}, {
  text: '已结束',
  value: 'finished'
}]

util.format = function (input, args) {
  if (typeof (args) !== 'object') args = [args]
  return input.replace(/{(\d+)}/g, function (match, number) {
    return typeof args[number] !== 'undefined' ? args[number] : match
  })
}

util.formatDate = function (input, fmt) {
  fmt = fmt || 'yyyy-MM-dd'
  const date = typeof input === 'string' ? new Date(input) : input
  var o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
    'q+': Math.floor((date.getMonth() + 3) / 3),
    S: date.getMilliseconds()
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
    }
  }
  return fmt
}

export default util
